<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数组基础练习</title>
    <script>
      let names = ['关羽', '张飞', '马超', '赵云', '黄忠', '刘备', '姜维']
      for (name of names) {
        console.log(name)
      }

      let nums = [2, 6, 1, 7, 4]
      let sum = 0
      for (num of nums) {
        sum += num
      }
      console.log(`元素和为${sum}, 平均值为: ${sum / nums.length}`)

      let nums2 = [2, 6, 1, 77, 52, 25, 7]
      //假设第一个元素是最大值
      let max = nums2[0]
      //从第二个元素开始遍历数组
      for (let i = 1; i < nums2.length; i++) {
        //依次和max比较,如果比max还大,则覆盖max的值
        if (nums2[i] > max) {
          max = nums2[i]
        }
      }
      console.log(`数组中最大的元素是: ${max}.`)

      let colors = ['red', 'green', 'blue', 'yellow']
      let str = ''
      for (let idx in colors) {
        /* if (idx == colors.length - 1) {
          str += colors[idx]
        } else {
          str += colors[idx] + '|'
        } */
        str += idx == colors.length - 1 ? colors[idx] : colors[idx] + '|'
      }
      console.log(str)
    </script>
  </head>
  <body></body>
</html>
